<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <!-- json.stringify(value[,replacer[,space]])   将javascript对象转字符串 -->
    <!-- value表示要转换的javascript值 -->

    <div id="demo">

    </div>

    <!-- javascript对象转字符串 -->
    <script>
        var obj = {'name':'runoob','alexa':1000,'site':'www.runoob.com'};
        // 将obj转成字符串
        var myjson = JSON.stringify(obj);
        // 将myjson发送到服务器
        document.getElementById('demo').innerHTML=myjson;
    </script>

    <!-- javascript数组转字符串 -->
    <script>
        var arr = ['goodle','taobao','facebook'];
        var myjson = JSON.stringify(arr);
        document.getElementById('demo').innerHTML = myjson;
    </script>

    <!-- 将日期转字符串 -->
    <script>
            var obj = {'name':'runoob','initDate':1000,'site':'www.runoob.com'};
            // 将obj转成字符串
            var myjson = JSON.stringify(obj);
            // 将myjson发送到服务器
            document.getElementById('demo').innerHTML=myjson;
    </script>

    <!-- 【不建议在json中使用函数】解析函数：json不能包含函数，json.stringify()会删除javascript对象的函数，包括key,value -->
    <script>
        var obj = { "name":"Runoob", "alexa":function () {return 10000;}, "site":"www.runoob.com"};
        // 将函数转字符串
        obj.alexa = obj.alexa.toString();
        // 将json对象转字符串
        var myJSON = JSON.stringify(obj);
        document.getElementById("demo").innerHTML = myJSON;
    </script>

    
</body>
</html>